package com.erp.erp_servers.Dictionary.imp;

import com.erp.erp_dao.Dictionary.IDictionaryDAO;
import com.erp.erp_entitys.Dictionary.DictionaryEntity;
import com.erp.erp_entitys.Dictionary.DictionaryEntityExample;
import com.erp.erp_entitys.Dictionary.DictionaryVoEntity;
import com.erp.erp_servers.Dictionary.IDictionary2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName ImpDictionaryServer
 * @Description 字典服务实现类
 * @Author songjizhen
 * @Date 2020-06-22 14:49
 * @Version 1.0
 */
@Service
public class ImpDictionary2Service  implements IDictionary2Service {

    @Autowired
    private IDictionaryDAO dictionaryDAO;

    @Override
    public long countByExample(DictionaryEntityExample example) {
        return dictionaryDAO.countByExample(example);
    }

    @Override
    public int deleteByExample(DictionaryEntityExample example) {
        return dictionaryDAO.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dictionaryDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(DictionaryEntity record) {
        return dictionaryDAO.insert(record);
    }

    @Override
    public int insertSelective(DictionaryEntity record) {
        return dictionaryDAO.insertSelective(record);
    }

    @Override
    public List<DictionaryEntity> selectByExample(DictionaryEntityExample example) {
        return dictionaryDAO.selectByExample(example);
    }

    /**
     * 根据字典分类项与分类小项查询出字典列表,此服务为字典获取通过服务，其他服务为字典维护服务
     *
     * @param type
     * @param lx
     * @return
     */
    @Override
    public List<DictionaryEntity> getValue(Integer type, Integer lx) {
        DictionaryEntityExample example=new DictionaryEntityExample();
        DictionaryEntityExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andLxEqualTo(lx);
        //编码不为null是取字典值，为null是为关联页面

            criteria.andCodeIsNotNull();

        List<DictionaryEntity> dictionaryEntities = dictionaryDAO.selectByExample(example);
        return dictionaryEntities;
    }

    @Override
    public DictionaryEntity selectByPrimaryKey(Integer id) {
        return dictionaryDAO.selectByPrimaryKey(id);
    }

    /**
     * 获取所有字典分类列表
     *
     * @return
     */
    @Override
    public List<DictionaryVoEntity> queryAlllx() {
        List<DictionaryVoEntity> list=new ArrayList<DictionaryVoEntity>();
        //取所有大类小类
        List<DictionaryEntity> dictionaryEntities = dictionaryDAO.queryAlllx();
        List<DictionaryEntity> dl = this.getValue(6, 24);//取大类信息
        List<DictionaryEntity> xl = this.getValue(6, 25);//取小类信息
        Map<String,String> map_dl=new HashMap<String,String>();
        Map<String,String> map_xl=new HashMap<String,String>();
        for(int i=0;i<dl.size();i++){
            map_dl.put(dl.get(i).getCode(),dl.get(i).getName());
        }
        for(int i=0;i<xl.size();i++){
            map_xl.put(xl.get(i).getCode(),xl.get(i).getName());
        }
        Integer type=0;
        Integer lx=0;
        for(int i=0;i<dictionaryEntities.size();i++){//拼装字典树
            DictionaryEntity dictionaryEntity = dictionaryEntities.get(i);
            Integer typesub=dictionaryEntity.getType();
            Integer lxsub=dictionaryEntity.getLx();
            if(type==typesub){
                continue;
            }else{
                type=typesub;
               if(lx==lxsub){
                   continue;
               }
               lx=lxsub;
               // lx=dictionaryEntity.getLx();
                DictionaryVoEntity vo=new DictionaryVoEntity();
                vo.setId(dictionaryEntity.getId());
                vo.setName(map_dl.get(type+""));
                vo.setCode(type+"");
                vo.setHref("");
                vo.setTitle(vo.getName());
                vo.setField("");
                vo.setChildren(dg(type,dictionaryEntities,map_xl));
                list.add(vo);
            }
        }
        return list;
    }
   /* private List<Integer> getDL(List<DictionaryEntity> dictionaryEntities){
        List<Integer> list=new ArrayList<Integer>();
        Integer dl=0;
        for(DictionaryEntity de:dictionaryEntities){
            if(dl==de.getType()){
                continue;
            }else{
                list.add(de.getType());
                dl=de.getType();
            }
        }
        return list;
    }
    private List<Integer> getXL(List<DictionaryEntity> dictionaryEntities){
        List<Integer> list=new ArrayList<Integer>();
        Integer dl=0;
        for(DictionaryEntity de:dictionaryEntities){
            if(dl==de.getLx()){
                continue;
            }else{
                list.add(de.getLx());
                dl=de.getLx();
            }
        }
        return list;
    }*/
    //递归取大类下所有小类
    private List<DictionaryVoEntity> dg(Integer dl,List<DictionaryEntity> list,Map<String,String> map_xl){
        List<DictionaryVoEntity> children=new ArrayList<DictionaryVoEntity>();
        Integer lx=0;
        for(int i=0;i<list.size();i++){
            DictionaryEntity de = list.get(i);

            if(de.getType()==dl ){
                if(lx==de.getLx()){
                    continue;
                }
                if(de.getRemark()==null){
                    continue;
                }
                if(de.getCode() !=null){
                    continue;
                }
                lx=de.getLx();
                DictionaryVoEntity vo=new DictionaryVoEntity();
                vo.setId(de.getId());
                vo.setName(map_xl.get(de.getLx()+""));
                vo.setCode(de.getLx()+"");
                vo.setTitle(vo.getName());
                vo.setField("");
                vo.setType(dl);
                vo.setLx(lx);
                vo.setHref(de.getRemark()); //此处用描述字段来存href
                if(vo.getName() !=null){
                    children.add(vo);
                }

            }
        }
        return children;
    }


    @Override
    public int updateByExampleSelective(DictionaryEntity record, DictionaryEntityExample example) {
        return dictionaryDAO.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(DictionaryEntity record, DictionaryEntityExample example) {
        return dictionaryDAO.updateByExample(record,example);
    }

    @Override
    public int updateByPrimaryKeySelective(DictionaryEntity record) {
        return dictionaryDAO.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DictionaryEntity record) {
        return dictionaryDAO.updateByPrimaryKey(record);
    }


    @Override
    public DictionaryEntity selectByParam(Integer type, Integer lx, String code) {
        return dictionaryDAO.selectByParam(type,lx,code);
    }
}
